<?php
include'Utils.php';
class PicsController extends AppController{
	
	// List of used component
	public $components = array('CenterInfo');
	
	/**
	 * Admin function used to display list image page
	 */
	public function index(){
		
		// Check session and redirect to login page
		Utils::checkSession($this);
		$this->set('admin', '1');
		
		//Get center information
		$centerInfo = $this->CenterInfo->getInfo();
		$this->set('center_info', $centerInfo);
		
		// Count total image
		$count = $this->Pic->find('count');
		$totalLine = ceil($count/3);
		// Create list used to display grid on page
		$imageArr = array();
		
		// 1st row
		$imageArr[0][0] = "";
		$imageArr[0][1] = "";
		$imageArr[0][2] = "";
		$imageArr[0][3] = "";
		
		// 2nd row
		$imageArr[1][0] = "";
		$imageArr[1][1] = "";
		$imageArr[1][2] = "";
		$imageArr[1][3] = "";
		
		// 3rd row
		$imageArr[2][0] = "";
		$imageArr[2][1] = "";
		$imageArr[2][2] = "";
		$imageArr[2][3] = "";
		
		// insert Pic object into list
		$listImage = $this->Pic->find('all');
		$row = 0;
		$col = 0;
		foreach ($listImage AS $image):
			if($col < 4){
				$imageArr[$row][$col] = $image['Pic']['m_img_url'];
				$col = $col + 1;
			}else{
				$col = 0;
				$row = $row + 1;
				$imageArr[$row][$col] = $image['Pic']['m_img_url'];
				$col = $col + 1;
			}
		endforeach;
		
		$this->set('imageList', $imageArr);
		
	}
	
	/**
	 * Admin function used to regist new image
	 */
	public function add(){
		// Check session and redirect to login page
		Utils::checkSession($this);
		$this->set('admin', '1');
		
		// Main process
		if(isset($_FILES['file'])){
			
			// Regist image into data base
			$this->set('fileName', $_FILES["file"]["name"]);
			$this->request->data['m_img_url'] = $_FILES["file"]["name"];
			$this->request->data['m_img_is_main_flash'] = "1";
			$this->request->data['m_img_lash_order'] = 0;
			
			// Upload image
			if($this->Pic->save($this->request->data)){
				// Upload temp file
				move_uploaded_file($_FILES["file"]["tmp_name"],
				"images/main_image/" .$_FILES["file"]["name"]);
				$this->redirect(array('action' => 'index'));
			}
		}else{
			$this->redirect(array(
				'controller' => 'Pics',
				'action' => 'index'
			));
		}
	}
	
	/**
	 * Admin function used to delete image
	 */
	public function delete(){
		
		// Check session and redirect to login page
		Utils::checkSession($this);
		$this->set('admin', '1');
		
		// Main process
		$imgName = isset($_POST['img_name']) ? $_POST['img_name'] : "";
		if($imgName != ""){ // Process delete
			$this->Pic->deleteAll(array('Pic.m_img_url' => $imgName), false);
			unlink("images/main_image/" . $imgName);
			$this->redirect(array('action' => 'index'));
		}else{ // Redirect to list page
			$this->redirect(array('action' => 'index'));
		}
	}
	
	/**
	 * 
	 * Function used to check if image existed???
	 * @param String $imageName
	 */
	public function checkExist(){
		$imageName = $_POST['image_name'];
		$count = 0;
		
		// Check if pic exist
		$count = $this->Pic->find('count', array(
			'conditions' => array('Pic.m_img_url' => $imageName)
		));
		
		// echo check result
		if($count > 0){
			echo $count. "," . $imageName. "Hình ảnh đã tồn tại"; die;
		}else{
			echo ""; die;
		}
	}
	
}	
?>